local LS = {}

local this = LS
local status = false

--Local Stuff
local Display_temperature = CreateClientConVar( "LS_Display_Temperature", "K", true, false ) 


local ls_sb_mode = false

local FairTemp_Min = 283
local FairTemp_Max = 308
local ls_habitat = 0
local ls_air = 0
local ls_tmp = 0
local ls_coolant = 0
local ls_energy = 0

local ScH	= ScrH()
local MidW	= ScrW() / 2
local Left	= MidW - 80 //70
local Left2	= MidW - 90 //80
local Right	= MidW + 80 //70
local H1	= ScH / 8
local H2	= ScH - H1
local H3	= H1 - 5
local TH	= { H2 + 5, H2 + 20, H2 + 35, H2 + 50 }
local Font	= "ScoreboardText"

local White	= Color(225,225,225,255)
local Black	= Color(0,0,0,100)
local Cold	= Color(0,225,255,255)
local Hot	= Color(225,0,0,255)
local Warn	= Color(255,165,0,255)

local function lifesupport_HUDPaint()
	if GetConVarString('cl_hudversion') == "" then
		local ply = LocalPlayer()
		if not ply or not ply:Alive() or (ply:GetActiveWeapon() and ply:GetActiveWeapon() == "Camera") then return end
		//Msg("Values In Draw: "..tostring(ls_sb_mode).."|"..ls_habitat.."|"..ls_air.."|"..ls_tmp.."|"..ls_coolant.."|"..ls_energy.."\n")
		if not ls_sb_mode then
			if ply:WaterLevel() > 2 then
				local Air = ls_air / 20
				local valcol = White
				if Air < 4 then valcol = Warn end
				draw.RoundedBox( 8, Left2 , H2, 180, 20, Black)
				draw.DrawText( "Air:",					Font, Left, H2 + 5, White,	0 )
				draw.DrawText( tostring(Air).."%",		Font, Right,H2 + 5, valcol,	2 )
			end
		else
			if ply:WaterLevel() > 2 or ls_habitat < 5 or (ls_tmp > 0 and not (ls_tmp >= FairTemp_Min and ls_tmp <= FairTemp_Max)) then
				local Temp = ls_tmp
				local Air = ls_air / 20
				local Coolant = ls_coolant / 20
				local Energy = ls_energy / 20
				local ValCol = { White, White, White, White }
				if		Temp < FairTemp_Min then ValCol[1] = Cold
				elseif	Temp > FairTemp_Max then ValCol[1] = Hot
				end
				if Air		< 4 then ValCol[2] = Warn end
				if Coolant	< 4 then ValCol[3] = Warn end
				if Energy	< 4 then ValCol[4] = Warn end
				draw.RoundedBox( 8, Left2 , H2, 180, H3, Black)
				local d_temp = Temp;
				local d_temp_type = "K";
				if string.upper(Display_temperature:GetString()) == "C" then
					d_temp = Temp - 273
					d_temp_type = "C"
				elseif string.upper(Display_temperature:GetString()) == "F" then
					d_temp = (Temp * (9/5)) - 459.67
					d_temp_type = "F"
				end
				draw.DrawText( "Temperature:",			Font, Left,	TH[1], White,		0 )
				draw.DrawText( tostring(d_temp).." "..d_temp_type,	Font, Right,TH[1], ValCol[1],	2 )
				draw.DrawText( "Air:",					Font, Left,	TH[2], White,		0 )
				draw.DrawText( tostring(Air).."%",		Font, Right,TH[2], ValCol[2],	2 )
				draw.DrawText( "Coolant:",				Font, Left,	TH[3], White,		0 )
				draw.DrawText( tostring(Coolant).."%",	Font, Right,TH[3], ValCol[3],	2 )
				draw.DrawText( "Energy:",				Font, Left,	TH[4], White, 		0 )
				draw.DrawText( tostring(Energy).."%",	Font, Right,TH[4], ValCol[4],	2 )
			end
		end
	end
end 

local function LS_umsg_hook1( um )
	//Msg("Receiving umsg1\n")
	ls_sb_mode = true
	ls_habitat = um:ReadFloat()
	ls_air = um:ReadShort()
	ls_tmp = um:ReadShort()
	ls_coolant = um:ReadShort()
	ls_energy = um:ReadShort()
	//Msg("Values: "..tostring(ls_sb_mode).."|"..ls_habitat.."|"..ls_air.."|"..ls_tmp.."|"..ls_coolant.."|"..ls_energy.."\n")
end
usermessage.Hook("LS_umsg1", LS_umsg_hook1) 

local function LS_umsg_hook2( um )
	//Msg("Receiving umsg2\n")
	ls_sb_mode = false
	ls_air = um:ReadShort()
end
usermessage.Hook("LS_umsg2", LS_umsg_hook2) 

--The Class
/**
	The Constructor for this Custom Addon Class
*/
function LS.__Construct()
	hook.Add("HUDPaint", "LS_Core_HUDPaint", lifesupport_HUDPaint)
	status = true
	CAF.AddHook("think3", lifesupport_think)
	local RD = CAF.GetAddon("Resource Distribution")
	RD.AddProperResourceName("energy", "Energy")
	RD.AddProperResourceName("water", "Water")
	RD.AddProperResourceName("nitrogen", "Nitrogen")
	RD.AddProperResourceName("hydrogen", "Hydrogen")
	RD.AddProperResourceName("oxygen", "Oxygen")
	RD.AddProperResourceName("carbon dioxide", "Carbon Dioxide")
	RD.AddProperResourceName("steam", "Steam")
	RD.AddProperResourceName("heavy water", "Heavy Water")
	RD.AddProperResourceName("liquid nitrogen", "Liquid Nitrogen")
	RD.AddProperResourceName("hot liquid nitrogen", "Heated Liquid Nitrogen")
	return true
	//return false , "No Implementation yet"
end

/**
	The Destructor for this Custom Addon Class
*/
function LS.__Destruct()
	hook.Remove("HUDPaint", "LS_Core_HUDPaint")
	CAF.RemoveHook("think3", lifesupport_think)
	status = false
	return true
	//return false , "No Implementation yet"
end

/**
	Get the required Addons for this Addon Class
*/
function LS.GetRequiredAddons()
	return {"Resource Distribution"}
end

/**
	Get the Boolean Status from this Addon Class
*/
function LS.GetStatus()
	return status
end

/**
	Get the Version of this Custom Addon Class
*/
function LS.GetVersion()
	return 3.08, "Beta"
end

/**
	Get any custom options this Custom Addon Class might have
*/
function LS.GetExtraOptions()
	return {}
end

/**
	Gets a menu from this Custom Addon Class
*/
function LS.GetMenu(menutype, menuname)//Name is nil for main menu, String for others
	local data = {}
	if not menutype then
		--Create Info Menu
		data["Info"] = {}
		local tmp = data["Info"];
		tmp["Test"] = {}
		tmp["Test"].localurl = "test/test.html";
		tmp["Test"].interneturl = "http://snakesvx.blogdns.com";
		--Create Help Menu
		data["Help"] = {}
		tmp = data["Help"];
		tmp["Test2"] = {}
		tmp["Test2"].localurl = "test/test.html";
		tmp["Test2"].interneturl = "http://snakesvx.blogdns.com";
		
	end
	return data
end

/**
	Get the Custom String Status from this Addon Class
*/
function LS.GetCustomStatus()
	return "Not Implemented Yet"
end

function LS.AddResourcesToSend()
	
end

/**
	Returns a table containing the Description of this addon
*/
function LS.GetDescription()
	return {
				"Life Support 3",
				"",
				""
			}
end
CAF.RegisterAddon("Life Support", LS, "2")


